package git.bankview.wl.report;

import java.util.*;
import java.text.*;
import java.sql.*;

import laya.semi.process.*;

import git.bankview.sl.db.*;
import git.bankview.wl.bean.LogonBean;
import git.bankview.sl.report.ReportCommon;

import org.apache.log4j.Logger;

public class ReportTransAuthCreator
	implements IGenericClassActivity
{
	private static Logger log = Logger.getLogger(ReportTransAuthCreator.class);

	private static String SQL_INITTRANSMETHOD = 
		"INSERT INTO transmethod select it.transid, tcm.code"
		+ " from infotrans it, typecodemessage tcm"
		+ " where it.transid = ? and tcm.type = " + ReportCommon.TCM_METHODTYPE
		+ " and tcm.code <> " + ReportCommon.METHOD_INPUT;
	
	private static String SQL_NEWROLETRANS = 
		"INSERT INTO roletrans select iu.userid, it.transid, tm.methodid, 0"
		+ " from infouser iu, infotrans it, transmethod tm"
		+ " where iu.userid = ? and it.transid = ? and tm.transid = ?";
		
	public void execute(IProcessContext oCtx)
		throws Exception
	{
		String[] vPropName = oCtx.getActivityParameter().split(":");
		LogonBean logon = (LogonBean)oCtx.getField(vPropName[0]);
		
		String oTransid = (String)oCtx.getField("transid");
		
		Connection oConn = DBConnectionFactory.getCoreConnection();
		try
		{
			// init transmethod table
			log.debug(SQL_INITTRANSMETHOD);
			PreparedStatement ps = oConn.prepareStatement(SQL_INITTRANSMETHOD);
			ps.setString(1, oTransid);
			ps.executeUpdate();
			
			// insert owner records in roletrans
			log.debug(SQL_NEWROLETRANS);
			ps = oConn.prepareStatement(SQL_NEWROLETRANS);
			ps.setString(1, logon.getUsername());
			ps.setString(2, oTransid);
			ps.setString(3, oTransid);
			ps.executeUpdate();
		}
		finally
		{
			oConn.close();
		}
	}
}
